package com.right.dao.impl;

import java.sql.Connection;
import java.util.List;

import com.Tools.DBTools;
import com.Tools.MD5Util;
import com.Tools.PageData;
import com.right.dao.UsersDao;
import com.right.entity.Users;

public class UsersDaoImpl implements UsersDao{
	MD5Util md5 = new MD5Util();
	/**
	 * 增加用户
	 * @param users 用户对象
	 * @return 操作成功;false 操作失败
	 */
	@Override
	public boolean addusers(Users users) {
		String sql = "insert into users values(null,?,?,?,?,?,0,0)";
		String userpwd = users.getUserpwd();
		String md5pwd =md5.getEncodeByMd5(userpwd);
		return DBTools.exUpdate(sql,users.getUsername(),md5pwd,users.getUsersex(),users.getAddress(),users.getUsercall()) > 0;

	}
	
	/**
	 * 删除用户
	 * @param username 字符串用户名
	 * @return 操作成功;false 操作失败
	 */
	@Override
	public boolean delusers(int userid) {
		String sql = "delete from users where userid = ?";
		return DBTools.exUpdate(sql, userid) > 0;
	}
	/**
	 * 登录验证
	 * @param username 字符串用户名，userpwd字符串密码
	 * @return 数组长度判断，大于0为真
	 */
	
	@Override
	public boolean login(String username, String userpwd) {
		String sql = "select userid from users where username = ? and userpwd = ?";
		String md5pwd = md5.getEncodeByMd5(userpwd);
		List<Users> list = (List<Users>)DBTools.exQuery(sql, Users.class, username,md5pwd);
		return list.size()>0;
	}
	
	/**
	 * 修改用户信息
	 * @param username 字符串用户名，userpwd字符串密码
	 * @return 操作成功;false 操作失败
	 */
	@Override
	public boolean updateusers(Users users) {
		
		String sql = "update users set username = ?,usersex = ?,address = ?,usercall = ? where userid = ?";
		return DBTools.exUpdate(sql, users.getUsername(),users.getUsersex(),users.getAddress(),users.getUsercall(),users.getUserid())>0;
	}
	
	/**
	 * 精确查询用户
	 * @param username用户名
	 * @return list数组内第一个用户对象
	 */
	@Override
	public Users getUserByName(String username) {
		String sql = "select * from users where username = ?";
		List<Users> list = (List<Users>) DBTools.exQuery(sql, Users.class, username);
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}
	
	@Override
	public Users getUserByid(int userid) {
		String sql = "select * from users where userid = ?";
		List<Users> list = (List<Users>) DBTools.exQuery(sql, Users.class, userid);
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}
	
	@Override
	public PageData<Users> getUserByPage(int page, int pageSize, String keywords) {
		PageData<Users> pd = DBTools.exQueryByPage("select * from users where username like ?", Users.class,
				page, pageSize, "%"+keywords+"%");
		return pd;
	}
	
	@Override
	public Users getIdByName(String username) {
		String sql ="select userid from users where username = ?";
		List<Users> list = (List<Users>) DBTools.exQuery(sql, Users.class, username);
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}
	
	@Override
	public int changemoney(Connection conn,double money,int userid) {
		String sql = "update users set money = money + ? where userid = ?";
		int n= 0;
		try {
			n = DBTools.exUpdate(sql, conn, money,userid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return n;
	}
	
	
}
